package com.example.myapplication;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * @description: Android原生页面
 */
public class NativePageActivity extends AppCompatActivity {
    private TextView tvText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_native_page);

        tvText = findViewById(R.id.tv_text);
        // Flutter页面传过来的参数
        tvText.setText("Flutter页面传过来的参数：name=" + getIntent().getStringExtra("name"));

        ((Button) findViewById(R.id.btn_back)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("message", "我从原生页面回来了");
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        ((Button) findViewById(R.id.btn_flutter)).setOnClickListener (v -> {
            Intent intent = new Intent(this, FlutterActivity.class);
            // 参数（JSON数据）
            intent.putExtra("data", "{\"message\": \"从原生进入flutter页面\"}");
            // 路由
            intent.putExtra("route", "empty");
            startActivityForResult(intent, 0);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 0:
                tvText.setText(data == null ? "null" : data.getStringExtra("message"));
                break;
            default:
                break;
        }
    }
}
